﻿using Capstone.Services;
using Capstone.Views;
using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Capstone.ViewModels;
using Xamarin.Essentials;

namespace Capstone
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            DependencyService.Register<MockDataStore>();
            DependencyService.Register<DiaryDataStore>();

            Console.WriteLine( Preferences.Get("gut_symptom_diary_q1", "1"));

            if (Application.Current.Properties.ContainsKey("Access_token"))
            {
                if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Refresh_token_exp"]))
                {
                    AboutViewModel objAboutViewModel = new AboutViewModel();
                    objAboutViewModel.OnLogout();
                }
                else if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Access_token_exp"]))
                {
                    LoginViewModel objLoginViewModel = new LoginViewModel();
                    objLoginViewModel.NewAccessToken();
                    GoAboutPage();
                }
                else
                {
                    GoAboutPage();
                }
            }
            else
            {
                MainPage = new AppShell();
            }
        }

        private async void GoAboutPage()
        {
            MainPage = new AppShell();
            await Shell.Current.GoToAsync($"//{nameof(AboutPage)}");
        }

        protected override void OnStart()
        {
            if (Application.Current.Properties.ContainsKey("Access_token"))
            {
                if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Refresh_token_exp"]))
                {
                    AboutViewModel objAboutViewModel = new AboutViewModel();
                    objAboutViewModel.OnLogout();
                }
                else if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Access_token_exp"]))
                {
                    LoginViewModel objLoginViewModel = new LoginViewModel();
                    objLoginViewModel.NewAccessToken();
                }
            }
        }

        protected override void OnSleep()
        {
            if (Application.Current.Properties.ContainsKey("Access_token"))
            {
                if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Refresh_token_exp"]))
                {
                    AboutViewModel objAboutViewModel = new AboutViewModel();
                    objAboutViewModel.OnLogout();
                }
                else if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Access_token_exp"]))
                {
                    LoginViewModel objLoginViewModel = new LoginViewModel();
                    objLoginViewModel.NewAccessToken();
                }
            }
        }

        protected override void OnResume()
        {
            if (Application.Current.Properties.ContainsKey("Access_token"))
            {
                if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Refresh_token_exp"]))
                {
                    AboutViewModel objAboutViewModel = new AboutViewModel();
                    objAboutViewModel.OnLogout();
                }
                else if (DateTime.UtcNow > Convert.ToDateTime(Application.Current.Properties["Access_token_exp"]))
                {
                    LoginViewModel objLoginViewModel = new LoginViewModel();
                    objLoginViewModel.NewAccessToken();
                }
            }
        }
    }
}
